package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.jeecg.modules.demo.com.kunpeng.entity.ContactsDetails;
import org.jeecg.modules.demo.com.kunpeng.entity.CustomerInformation;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CustomerInformationUpdateDTO;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.CustomerQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.ContactsDetailsMapper;
import org.jeecg.modules.demo.com.kunpeng.mapper.CustomerInformationMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ICustomerInformationService;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationPage;
import org.jeecg.modules.demo.com.kunpeng.vo.CustomerInformationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class CustomerInformationServiceImpl extends ServiceImpl<CustomerInformationMapper, CustomerInformation> implements ICustomerInformationService {

    @Autowired
    private CustomerInformationMapper customerInformationMapper;

    @Autowired
    private ContactsDetailsMapper contactsDetailsMapper;

    /**
     * 插入一条客户信息
     * @param customerInformationPage
     */
    @Override
    public void insert(CustomerInformationPage customerInformationPage) {
        CustomerInformation customerInformation=new CustomerInformation();
        BeanUtils.copyProperties(customerInformationPage,customerInformation);

        log.info("插入一条客户信息：service");
        String id = customerInformation.getId();
        CustomerInformationVO customerInformationVO = customerInformationMapper.selectById(id);
        if(customerInformationVO!=null){
            if(customerInformationVO.getCustomerName().equals(customerInformation.getCustomerName())){
                String message="客户已存在！";
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        int num = customerInformationMapper.insert(customerInformation);
        if(num!=1){
            String message="系统繁忙，请稍后重试！";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }

        ContactsDetails contactsDetails=new ContactsDetails();
        BeanUtils.copyProperties(customerInformationPage,contactsDetails);
        contactsDetails.setCiId(customerInformation.getId());
        contactsDetailsMapper.insert(contactsDetails);

    }

    /**
     * 删除一条客户信息
     * @param id
     */
    @Override
    public void deleteById(String id) {
        log.info("删除一条客户信息：service");
        CustomerInformationVO customerInformationVO = customerInformationMapper.selectById(id);
        if(customerInformationVO==null){
            String message="客户已删除！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int num = customerInformationMapper.deleteById(id);
        if(num!=1){
            String message="系统繁忙，请稍后重试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    /**
     * 批量删除客户信息
     * @param ids
     */
    @Override
    public void deleteBatch(String[] ids) {
        log.info("批量删除客户信息：service");
        int num = customerInformationMapper.deleteBatch(ids);
        if(num<=0){
            String message="系统繁忙，请稍后重试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    /**
     * 根据id修改客户信息
     * @param customerInformationUpdateDTO
     */
    @Override
    public void updateById(CustomerInformationUpdateDTO customerInformationUpdateDTO) {
        log.info("根据id修改客户信息：service");
        CustomerInformation customerInformation=new CustomerInformation();
        BeanUtils.copyProperties(customerInformationUpdateDTO,customerInformation);
        int num = customerInformationMapper.updateById(customerInformation);
        if(num!=1){
            String message="系统繁忙，请稍后重试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    /**
     * 分页查询客户信息
     * @param page
     * @return
     */
    @Override
    public Page<CustomerInformationVO> list(Page<CustomerInformationVO> page, CustomerQueryListDTO customerQueryListDTO) {
        log.info("分页查询客户信息：service");
        Page<CustomerInformationVO> customerInformationVOPage = page.setRecords(customerInformationMapper.list(page,customerQueryListDTO.getCustomerGrade(),
                                                                                customerQueryListDTO.getCustomerSource(),customerQueryListDTO.getCustomerIntent(),customerQueryListDTO.getState(),customerQueryListDTO.getKeyWords()));
        if(customerInformationVOPage.getRecords().size()==0){
            String message="系统繁忙，请稍后重试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return customerInformationVOPage;
    }

    /**
     * 获取客户名称列表
     * @return
     */
    @Override
    public List<String> listCustomerName() {
        List<String> customerNameList = customerInformationMapper.listCustomerName();
        return customerNameList;
    }

    /**
     * 废弃客户列表
     * @param page
     * @param
     * @return
     */
    @Override
    public Page<CustomerInformationVO> discardCustomerList(Page<CustomerInformationVO> page, CustomerQueryListDTO customerQueryListDTO) {
        Page<CustomerInformationVO> customerInformationVOPage = page.setRecords(customerInformationMapper.discardCustomerList(page, customerQueryListDTO.getKeyWords()));
        if(customerInformationVOPage.getRecords().size()==0){
            String message="没有符合条件的数据，请重试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return customerInformationVOPage;
    }
}
